<?php
namespace Com\Pay;


class benfuBankPay{

    private $config = array();

    public function __construct($data){
        $this->config = $data;
    }


    /*生成支付连接*/
    public function getUrl($order_info){
        $merchantCode = $this->config['mch_id'];

        $key    = $this->config['mch_key'];

        $price = $order_info['paymoney'];

        $returnUrl = $order_info['return_url'];

        $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/BenfuNotify';

        $order_sn = $order_info['ordernumber'];
        $post_data = array(
            'p1_MerchantNo' =>   $merchantCode,
            'p2_OrderNo'    =>     $order_sn,
            'p3_Amount'     =>  $price,
            "p4_Cur"        =>  1,
            "p5_ProductName"  =>  'ChangbaoBank',
            "p7_ReturnUrl"  =>  $returnUrl,
            "p8_NotifyUrl"  =>  $notifyUrl,
            "p9_FrpCode" => $order_info['defaultbank'],
            'pa_OrderPeriod' => '0'
        );
//        dump($post_data);die;
        $post_data['hmac'] = MD5(implode('',$post_data).$key);
        $tar_url = 'http://gateway.bendpay.com/gateway_init.action';
        $res = $this->curl_post($tar_url,$post_data);
        return $res;
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $key    = $this->config['mch_key'];
        $data = $_REQUEST;
        $signy = $data['hmac'];
        unset($data['t']);unset($data['f']);
        unset($data['hmac']);
        $data['ra_PayTime'] = urldecode($data['ra_PayTime']);
        $data['rb_DealTime'] = urldecode($data['rb_DealTime']);
        $sign = MD5(implode('',$data).$key);
        if($signy == $sign){
            if($data['r6_Status'] == '100'){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }


    public function curl_json_post($url,$data)
    {
        if(is_array($data)){
            $data_string = json_encode($data);
        }else{
            $data_string = $data;
        }
        // echo $data_string;die;
        $ch = curl_init($url);
        // curl_setopt($ch, CURLOPT_TIMEOUT,5);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
        );
        $result = curl_exec($ch);
        curl_close ( $ch );
        return $result;
    }

    public function curl_post($url, $data)
    {
        $data = http_build_query($data);
        // file_put_contents('./tmp.txt',$data);
        //echo '<br>'.$data.'<br>';
        // mb_strlen($str,'utf-8');
        // var_dump((mb_strlen($data,'utf-8') == strlen($data)));
        $opts = array(
            'http'=>array(
                'method'=>"POST",
                'timeout'=>60,
                'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
                    "Content-length:".mb_strlen($data,'utf-8')."\r\n" .
                    "Cookie: foo=bar\r\n" .
                    "\r\n",
                'content' => $data,
            )
        );
        $cxContext = stream_context_create($opts);
        $result = file_get_contents($url, false, $cxContext);
        return $result;
    }

    private function gbkToUtf8($arr){
        if(is_array($arr)){
            $res = array();
            foreach($arr as $k => $v){
                $res[$k] = $this->gbkToUtf8($v);
            }
            return $res;
        }else{
            return mb_convert_encoding ( $arr, "UTF-8", "GBK" );
        }
    }


}
